﻿  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Data;
  using System.Xml;
  using System.Xml.Serialization;


 public    class XmlUtil
    {

  #region 反序列化
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="xml">XML字符串</param>
    /// <returns></returns>
    public static object Deserialize(Type type, string xml)
    {
        try
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }
        catch (Exception e)
        {
 
            return null;
        }
    }
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="type"></param>
    /// <param name="xml"></param>
    /// <returns></returns>
    public static object Deserialize(Type type, Stream stream)
    {
        XmlSerializer xmldes = new XmlSerializer(type);
        return xmldes.Deserialize(stream);
    }
    #endregion
 
    #region 序列化
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="obj">对象</param>
    /// <returns></returns>
    public static string Serializer(Type type, object obj)
    {
        //准备序化列对象
        XmlSerializer xs = new XmlSerializer(obj.GetType());
        MemoryStream ms = new MemoryStream();
        //设置序序化XML格式
        XmlWriterSettings xws = new XmlWriterSettings();
        xws.Indent = true;
        xws.OmitXmlDeclaration = true;
        xws.Encoding = Encoding.UTF8;
        XmlWriter xtw = XmlTextWriter.Create(ms, xws);
        //去掉要结点的 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 属性
        XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(
            new XmlQualifiedName[] {
                        new XmlQualifiedName(string.Empty, "aa")
         });
        xs.Serialize(xtw, obj, _namespaces);
        ms.Position = 0;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(ms);
        //给文档添加<?xml version="1.0" encoding="utf-8"?>
        XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
        xmlDoc.InsertBefore(xmlDecl, xmlDoc.DocumentElement);

        // MemoryStream Stream = new MemoryStream();
        //// XmlSerializer xml = new XmlSerializer(type);
        // try
        // {
        //     StreamWriter textWriter = new StreamWriter(Stream, Encoding.GetEncoding("UTF-8"));
        //     XmlSerializer serializer = new XmlSerializer(type);
        //     serializer.Serialize(textWriter,obj);
        //     //序列化对象
        //   //  serializer.Serialize(Stream, obj);
        //     string xmlMessage = Encoding.UTF8.GetString(Stream.GetBuffer());

        // }
        // catch (InvalidOperationException)
        // {
        //     throw;
        // }
        //ms.Position = 0;
        //StreamReader sr = new StreamReader(ms);
        //string str = sr.ReadToEnd();

        //sr.Dispose();
        //ms.Dispose();

        string str = xmlDoc.InnerXml;

        return str;
    }
 
    #endregion
    }

